# -*- coding: utf-8 -*-
"""
Created on Wed Oct 11 10:30:38 2017

@author: Luther
"""

import re
import os


def positive(text):
    positive_list = re.findall(r'【.+?】', text)
    return positive_list


def negative(text):
    negative_list = re.findall(r'《.+?》', text)
    return negative_list


file_list = os.listdir('D:\\input\\')
n = 1
for file in file_list:
    print('正在打开第', n, '份文件')
    with open(os.path.join('D:\\input\\', file)) as f:
        text = f.read()
    positive_list = positive(text)
    for i in positive_list:
        with open('D:\\result.csv', 'a') as f1:
            f1.write(file)
            f1.write(',')
            f1.write(i)
            f1.write(',')
            f1.write('1')
            f1.write('\n')
    print('已写入积极词汇!')
    negative_list = negative(text)
    for i in negative_list:
        with open('D:\\result.csv', 'a') as f1:
            f1.write(file)
            f1.write(',')
            f1.write(i)
            f1.write(',')
            f1.write('0')
            f1.write('\n')
    print('已写入消极词汇!')
    n = n + 1

print('Finished!')
